/*
   Learn how to use signal &sigaction &curses function.
@cruise
*/

#include <stdlib.h>
#include <stdio.h>
#include <curses.h>
#include <sys/time.h>
#include <signal.h>
#define  MSG    "mgl"
#define  MSG1   "ywf"
#define  BLANK  "   "
#define  ROK        "======="
#define  ROK_BLANK  "       "
struct position_t
{
    int row;
    int col;
    int row_idx;
    int col_idx;    
}position;
struct rok_t
{
    /* data */
    int col;
    int col_idx;       
}rok;

void settimer(int time);
int interval_time =100;  //200ms;
void alarm_hanlder(int num);
//FILE *file;
int main()
{
    char ch;
    position.row =10;
    position.col =10;
    position.row_idx=1;
    position.col_idx =1;
    rok.col =1;
    //file = fopen("tmp.dat","a+");
    initscr();
    crmode();
    noecho();
    clear();
    move(position.row,position.col);
    addstr(MSG);
    move(LINES-1,1);
    addstr(ROK);    
    settimer(interval_time);
    signal(SIGALRM,alarm_hanlder);
    while (1)
    {
        /* code */
        ch = getch();
        switch (ch)
        {
        case 'Q': 
            break;
        case 'h':
            rok.col_idx = -5;
            break;
        case 'l':
            rok.col_idx =5;
            break;
        
        default:
            rok.col_idx=0;
            break;
        }
    }
    //fprintf(file,"end op");
   // fclose(file);
    endwin();
    exit(0);
}
void settimer(int time)
{
    struct itimerval tm;
    int second;
    int microsecond;
    second = time/1000;
    microsecond = (time%1000)*1000;
    tm.it_interval.tv_sec = second;
    tm.it_interval.tv_usec = microsecond;
    tm.it_value.tv_sec =second;
    tm.it_value.tv_usec=microsecond;
    setitimer(ITIMER_REAL,&tm,NULL);
}
void alarm_hanlder(int num)
{
        static  int cnt=0;
        if(cnt++%2 ==0)
        {
            move(position.row,position.col);
            addstr(BLANK);
            position.col = position.col + position.col_idx;
            position.row = position.row + position.row_idx;
            move(position.row,position.col);
          //  fprintf(file,"row is %d,col is %d \n",position.row,position.col);
          //  fflush(file);       
            addstr(MSG1);  
            refresh();			/* and show it		*/
            if(position.col>=COLS-1)  // be careful not to spill
            {
                position.col_idx =-1;
            }   
            if(position.col<=1)
            {
                position.col_idx = 1;
            }        
            if(position.row >=LINES-1)
            {
                position.row_idx =-1;
            }
            if(position.row <=1)
            {
                position.row_idx = 1;
            }   
        }
        if(rok.col_idx !=0 )
        {
        //move rod
            move(LINES-1,rok.col);
            addstr(ROK_BLANK);
            rok.col = rok.col + rok.col_idx;
            rok.col_idx=0;
            if(rok.col <=0)
            {
                rok.col =0;
            }
            if(rok.col >=COLS)
            {
                rok.col =COLS;
            }
            move(LINES-1,rok.col);
            addstr(ROK);
          //  fprintf(file,"ROK  row is %d,col is %d \n",LINES,rok.col);
          //  fflush(file);         
            refresh();			/* and show it		*/
        }

}